<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:pro="http://xml.netbeans.org/schema/products" xmlns:user="http://xml.netbeans.org/schema/users" xmlns:userdetail="http://xml.netbeans.org/schema/userdetails" xmlns:order="http://xml.netbeans.org/schema/orders" xmlns:orderdetail="http://xml.netbeans.org/schema/orderdetails" exclude-result-prefixes="pro user userdetail order orderdetail" version="1.0">
    <xsl:output method="html" indent="yes" version="1.0"/>
    <xsl:param name="type" select="'inorder'"/>
    <xsl:param name="userpath" select="''"/>
    <xsl:param name="userdetailpath" select="''"/>
    <xsl:param name="orderpath" select="''"/>
    <xsl:param name="orderidresponse" select="''"/>
    <xsl:param name="propath" select="''"/>
    <xsl:variable name = "userdoc" select="document($userpath)"/>
    <xsl:variable name = "userdetaildoc" select="document($userdetailpath)"/>
    <xsl:variable name = "orderdoc" select="document($orderpath)"/>
    <xsl:variable name = "prodoc" select="document($propath)"/>

    <xsl:template match="/">
        <xsl:if test="$type='inorder'">
            <xsl:call-template name="inorder"/>
        </xsl:if>
        <xsl:if test="$type='inorderdetail'">
            <xsl:call-template name="inorderdetail"/>
        </xsl:if>
    </xsl:template>
    <xsl:template name="inorder">
        <table border="1" class="table-hover">
            <thead align="center">
                <tr>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Payment by</th>
                    <th>Date</th>
                    <th>Total price(USD)</th>
                    <th>Status</th>
                    <th>
                    </th>
                </tr>
            </thead>
            <tbody align="center">
                <xsl:for-each select="order:Orders/order:Order">
                    <xsl:sort select="order:Date" order="descending" data-type="Date" />
                    <xsl:variable name="userid" select="order:UserId"/>
                    <xsl:variable name="userNode" select="$userdoc/user:Users/user:User[user:Id=$userid]"/>
                    <xsl:variable name="userdetailid" select="$userNode/user:UserDetailId"/>
                    <xsl:variable name="userdetailNode" select="$userdetaildoc/userdetail:UserDetails/userdetail:UserDetail[userdetail:Id=$userdetailid]"/>
                    <tr>
                        <td>
                            <div>
                                <xsl:value-of select="$userdetailNode/userdetail:Name"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="$userdetailNode/userdetail:PhoneNum"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="order:PaymentMethod"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="substring-before(order:Date,'T')"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="order:TotalPriceAll"/>
                            </div>
                        </td>
                        <xsl:variable name="options">
                            <selected>
                                <xsl:value-of select="order:Status"/>
                            </selected>
                            <opt>Done</opt>
                            <opt>On-Going</opt>`
                            <opt>Cancel</opt>
                        </xsl:variable>
                        <td class="manage-content-order-status" >
                            <select orderid="{order:Id}" class="linkUpdateStatusOrder">
                                <xsl:for-each select="exsl:node-set($options)/opt">
                                    <option value="{.}">
                                        <xsl:if test="exsl:node-set($options)/selected=.">
                                            <xsl:attribute name="selected">
                                                <xsl:text>selected</xsl:text>
                                            </xsl:attribute>
                                        </xsl:if>
                                        <xsl:value-of select="."/>
                                    </option>
                                </xsl:for-each>
                            </select>
                        </td>
                        <td>
                            <a class="btn btn-info linkDetail">
                                <xsl:attribute name="orderid">
                                    <xsl:value-of select="order:Id" />
                                </xsl:attribute>
                                <xsl:text>View Detail</xsl:text>
                            </a>
                        </td>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template name="inorderdetail">
        <h3>Order Detail</h3>
        <table border="1" class="table table-hover">
            <thead>
                <tr>
                    <td colspan="5">
                        ID:
                        <xsl:value-of select="$orderidresponse"/>
                    </td>
                </tr>
                <tr>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Quantity</th>
                    <th colspan="2">Total price</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="orderdetail:OrderDetails/orderdetail:OrderDetail[orderdetail:OrderId=$orderidresponse]">
                    <xsl:variable name="productid" select="orderdetail:ProductId"/>
                    <xsl:variable name="productNode" select="$prodoc/pro:Products/pro:Product[pro:Id=$productid]"/>
                    <tr>
                        <td>
                            <div>
                                <xsl:value-of select="$productNode/pro:Name"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="orderdetail:Price"/>
                            </div>
                        </td>
                        <td>
                            <div>
                                <xsl:value-of select="orderdetail:Quantity"/>
                            </div>
                        </td>
                        <td colspan="2">
                            <div>
                                <xsl:value-of select="orderdetail:TotalPrice"/>
                            </div>
                        </td>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>
</xsl:stylesheet>
